#!/bin/bash

# check if testbox has enough memory to run the job

source $LKP_SRC/lib/unit.sh

# if need_memory=100% and nr_cpu is 64
# it means it needs 64G memory at least
[[ $need_memory =~ %$ ]] && {
	[[ -n "$nr_cpu" && $nr_cpu -ge 1 ]] || {
		echo "nr_cpu is not correct at hosts/$testbox"
		exit 1
	}

	ratio=${need_memory%\%*}
	need_memory=$((nr_cpu * ratio / 100))G
}

need_memory_mb=$(to_mb $need_memory)

# get available memory(mb)
# liuyd@lkp-boy:~$ free -m
#               total        used        free      shared  buff/cache   available
# Mem:          16051        2251       12407          17        1391       13487
# Swap:          2045           0        2045

available_memory_mb=$(free -m | sed -n "2, 1p" | awk '{print $7}')

if ((need_memory_mb > available_memory_mb)); then
	echo "rm $job_file due to not enough memory"
	echo "need: $need_memory_mb MB, has: $available_memory_mb MB"
	exit 1
fi

exit 0
